package com.ww.idiom.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.core.util.DeviceUtil;
import com.ww.core.util.StringUtils;
import com.ww.core.util.email.EmailUtils;
import com.ww.core.widget.dialog.MsgDialog;
import com.ww.idiom.R;
import com.ww.idiom.activity.menu.LeftFragment;
public class FeedBackActivity extends SlideMenuActivity {
	private EditText email, content;
	private boolean b = true;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			if (b) {
				MsgDialog.show(context, R.string.feedback_success);
			} else {
				MsgDialog.show(context, R.string.feedback_fail);
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_feedback, container,false);
		initView(view);
		return view;
	}
	

	private void initView(View view) {
		setTitle(view, LeftFragment.mTitleName);
		email = (EditText) view.findViewById(R.id.email);
		content = (EditText)view. findViewById(R.id.content);
		Button send = (Button)view. findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (DeviceUtil.checkNet(context)) {
					if (StringUtils.isNotEmpty(content.getText().toString())) {
						if (StringUtils.isNotEmpty(email.getText().toString())) {
							if (email.getText().toString().contains("@")) {
								showProgressDialog(context);
								new Thread(new Runnable() {
									@Override
									public void run() {
										EmailUtils e = new EmailUtils(context, email.getText().toString());
										b = e.sendEmail(content.getText().toString());
										handler.sendEmptyMessage(1);
									}
								}).start();
							} else {
								MsgDialog.show(context, R.string.feedback_email_format);
							}
						} else {
							MsgDialog.show(context, R.string.feedback_email_format);
						}
					} else {
						MsgDialog.show(context, R.string.feedback_content_empty);
					}
				}
			}
		});
	}
}
